# ==========================用户修改区========================
# 编译工具
HOST = 
CC = $(HOST)gcc
AR = $(HOST)ar
# 编译参数
CFLAGS = -fPIC -Wall -c
# 外部库路径及链接的库
# 关于链接库的说明，会优先链接写在前面的库，且使用的库也是优先使用前面的
LDFLAGS=-L.
ifeq ($(OS), Windows_NT)
	LIBS = -lwsock32 -lws2_32 -lIPHLPAPI -lpsapi
else 
	
endif
# 输出目标名（最后输出的可执行文件名）
TARGET = broadcast
# 输出目标存放的路径（需要输出可执行文件存放的路径）
TARGET_FOLDER = ./bin
# 源文件路径(根据实际修改)
SRC_PATH = ./src
# 头文件路径(根据需要增删路径)
INCLUDES = ./src
#生成.o文件的目录
OBJ_FOLDER = ./obj
# 生成.so文件的目录
LIBS_FOLDER = ./bin
# 定义全局宏(根据需要增删宏)
DEFINES = DEBUG=0
# 安装路径
PREFIX = ./install
# service跟脚本路径
SERVICE_PATH = ./system

#============================================================

# 编译成库
ifeq ($(OS), Windows_NT)
	TARGETSO = $(TARGET).dll
	TARGETA = $(TARGET).lib
else 
	TARGETSO = lib$(TARGET).so
	TARGETA = lib$(TARGET).a
endif

#============================================================
# LIBS=-lpthread 
# -Wl,-rpath=/path/to/library1:/path/to/library2 设置程序运行后自动寻找库的路径，冒号隔开多个路径
# -Wl,-Bstatic -lmylibrary -Wl,-Bdynamic	设置链接时是链接动态库还是静态库
#============================================================

SOURCE_FILES_C= $(wildcard $(SRC_PATH)/*.c)
SOURCE_FILES_CXX= $(wildcard $(SRC_PATH)/*.cpp)
PURE_SOURCE_FILES= $(notdir $(SOURCE_FILES_C)) $(notdir $(SOURCE_FILES_CXX))

INCLUDE_PREFIX=-I#前缀
ALL_INCLUDES=$(addprefix $(INCLUDE_PREFIX), $(INCLUDES))

# 生成.o的文件
OBJ_FILES_C = $(patsubst %.c,$(OBJ_FOLDER)/%.o,$(PURE_SOURCE_FILES))
OBJ_FILES = $(patsubst %.cpp,$(OBJ_FOLDER)/%.o,$(OBJ_FILES_C))

DEFINE_PREFIX=-D#前缀
ALL_DEFINES=$(addprefix $(DEFINE_PREFIX), $(DEFINES))

# 这块就把源文件编译成o文件,可能是因为目标不是一个确定的内容或者单个内容，所以这里不会被直接执行
$(OBJ_FOLDER)/%.o : $(SRC_PATH)/%.c 
	$(CC) $(ALL_DEFINES) $(ALL_INCLUDES) $(LDFLAGS) $(CFLAGS) $< -o $@
$(OBJ_FOLDER)/%.o : $(SRC_PATH)/%.cpp 
	$(CC) $(ALL_DEFINES) $(ALL_INCLUDES) $(LDFLAGS) $(CFLAGS) $< -o $@

SERVICE_PATH_INSTALL :=
ifeq ($(shell test -d /usr/lib/systemd/system && echo true),true)  
	SERVICE_PATH_INSTALL := /usr/lib/systemd/system
else  
ifeq ($(shell test -d /lib/systemd/system && echo true),true)  
	SERVICE_PATH_INSTALL := /lib/systemd/system
else  
# 如果上面两个地址都不存在，则在下方自行填写适合的位置	
endif  
endif  

.PHONY :all clean lib install service uninstall showlibpary

all:prep create

prep:
# 如果不存在文件夹,则新建
	@if test ! -d $(OBJ_FOLDER); then mkdir $(OBJ_FOLDER); fi	
	@if test ! -d $(TARGET_FOLDER); then mkdir $(TARGET_FOLDER); fi
	@if test ! -d $(LIBS_FOLDER); then mkdir $(LIBS_FOLDER); fi

create:$(OBJ_FILES)	
# create只是一个执行的节点，后面代表执行这里所需要的条件，这样才会一步步依赖执行上面的.c生成.o 
	$(CC) $(LDFLAGS) -o $(TARGET_FOLDER)/$(TARGET) $(OBJ_FILES) $(LIBS)

show:
	@echo $(OBJ_FILES)
	@echo $(SERVICE_PATH_INSTALL)

showlibpary:
	@echo 'export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$$PWD'

lib:prep $(OBJ_FILES)
	$(CC) -shared -fPIC $(LDFLAGS) -o $(LIBS_FOLDER)/$(TARGETSO) $(OBJ_FILES) $(LIBS)
	$(AR) rcs $(LIBS_FOLDER)/$(TARGETA) $(OBJ_FILES)

install:all lib
	@if test ! -d $(PREFIX); then mkdir $(PREFIX); fi
	@if test ! -d $(PREFIX)/bin; then mkdir $(PREFIX)/bin; fi
	@if test ! -d $(PREFIX)/lib; then mkdir $(PREFIX)/lib; fi
	@if test ! -d $(PREFIX)/include; then mkdir $(PREFIX)/include; fi
	cp $(TARGET_FOLDER)/$(TARGET) $(PREFIX)/bin
	cp $(LIBS_FOLDER)/$(TARGETSO) $(LIBS_FOLDER)/$(TARGETA) $(PREFIX)/lib
	cp $(INCLUDES)/broadcast.h $(PREFIX)/include
	@if [ "$(PREFIX)" != "/usr" ]; then \
		echo "Prefix isn't /usr, service will not install"; \
		exit 0; \
	else \
		$(MAKE) service; \
	fi

uninstall:
	rm -f $(PREFIX)/bin/$(TARGET)
	rm -f $(PREFIX)/lib/$(TARGETSO) $(PREFIX)/lib/$(TARGETA)
	rm -f $(PREFIX)/include/broadcast.h
	@if [ "$(PREFIX)" = "/usr" ]; then \
		rm -f $(SERVICE_PATH_INSTALL)/brocli.service; \
		rm -f /usr/lib/$(TARGET)/*.sh; \
		rm -f /usr/lib/$(TARGET); \
	fi
	

# 建议用sudo执行这块
service:all lib
	@if [ ! -d "$(SERVICE_PATH)" ]; then echo "error: folder $(SERVICE_PATH) not exist"; exit 1; fi	
	@if [ ! -d "/usr/lib/$(TARGET)" ]; then mkdir /usr/lib/$(TARGET); fi
	@if [ -z "$(SERVICE_PATH_INSTALL)" ]; then echo "SERVICE_PATH_INSTALL is empty, setting in 'else' please"; exit 1; fi
	cp $(TARGET_FOLDER)/$(TARGET) /usr/bin/
	cp $(SERVICE_PATH)/*.service $(SERVICE_PATH_INSTALL)
	cp $(SERVICE_PATH)/*.sh /usr/lib/$(TARGET)
	chmod 777 /usr/lib/$(TARGET)/*.sh
	systemctl daemon-reload

clean:
	rm -f $(OBJ_FOLDER)/*.o
	rm -f $(TARGET_FOLDER)/$(TARGET) $(LIBS_FOLDER)/$(TARGETSO) $(LIBS_FOLDER)/$(TARGETA) 



